<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>API测试</title>
    <style>
        body { font-family: Arial, sans-serif; margin: 20px; }
        .test-result { margin: 10px 0; padding: 10px; border-radius: 5px; }
        .success { background-color: #d4edda; color: #155724; }
        .error { background-color: #f8d7da; color: #721c24; }
        button { padding: 10px 20px; margin: 5px; }
    </style>
</head>
<body>
    <h1>API连接测试</h1>
    
    <button onclick="testAPI()">测试API连接</button>
    <button onclick="testCities()">测试城市数据</button>
    <button onclick="testChat()">测试AI聊天</button>
    
    <div id="results"></div>

    <script>
        const API_BASE = 'http://localhost:5000/api';
        
        function addResult(message, isSuccess = true) {
            const div = document.createElement('div');
            div.className = `test-result ${isSuccess ? 'success' : 'error'}`;
            div.textContent = message;
            document.getElementById('results').appendChild(div);
        }
        
        async function testAPI() {
            try {
                const response = await fetch(`${API_BASE}/test`);
                const data = await response.json();
                addResult(`✅ API连接成功: ${data.message}`);
            } catch (error) {
                addResult(`❌ API连接失败: ${error.message}`, false);
            }
        }
        
        async function testCities() {
            try {
                const response = await fetch(`${API_BASE}/cities`);
                const data = await response.json();
                if (data.success) {
                    addResult(`✅ 城市数据加载成功: ${data.cities.length}个城市`);
                    addResult(`📊 数据来源: ${data.source}`);
                    if (data.api_response) {
                        addResult(`🔗 API状态: ${data.api_response.status} | 信息: ${data.api_response.info}`);
                    }
                    
                    // 显示前10个城市作为示例
                    const sampleCities = data.cities.slice(0, 10).map(city => city.name).join(', ');
                    addResult(`🏙️ 示例城市: ${sampleCities}...`);
                    
                    console.log('完整城市数据:', data.cities);
                } else {
                    addResult(`❌ 城市数据加载失败`, false);
                }
            } catch (error) {
                addResult(`❌ 城市数据请求失败: ${error.message}`, false);
            }
        }
        
        async function testChat() {
            try {
                const response = await fetch(`${API_BASE}/chat`, {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json',
                    },
                    body: JSON.stringify({
                        message: '明天从北京出发去上海'
                    })
                });
                const data = await response.json();
                if (data.success) {
                    addResult(`✅ AI聊天测试成功`);
                    console.log('AI回复:', data.response);
                } else {
                    addResult(`❌ AI聊天测试失败`, false);
                }
            } catch (error) {
                addResult(`❌ AI聊天请求失败: ${error.message}`, false);
            }
        }
        
        // 页面加载时自动测试
        window.onload = function() {
            addResult('页面加载完成，开始测试...');
            testAPI();
        };
    </script>
</body>
</html>